<script lang="ts" setup>
import { ref, onMounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import formBox from '@/components/queryTable/form/index.vue'
import tableBox from '@/components/queryTable/table/index.vue'
import { formItem } from '../form-item'
import gdglService from '@/api/gdgl'
import { TableItem } from '@/api/common'
import router from '@/router/index'
// 入参
// loading
const loading = ref<boolean>(false)
// 更多条件查询
const formLabel = ref()
// 生命周期
onMounted(async () => {
  loading.value = true
  formLabel.value = await formItem('yshList')
  loading.value = false
  // 分页
  changePageNum(1)
})

const total = ref<number>(0)
const page = computed(() => {
  return {
    pageNum: formData.value.pageNum,
    pageSize: formData.value.pageSize,
    total: total.value,
  }
})

// 查询条件
const formData = ref({
  pageSize: 20,
  pageNum: 1,
  filter: {
    bmsah: '',
    tysah: '',
    ajmc: '',
    gdr: '',
    sfzzajdm: '',
    sfyspdm: '',
    tjksrq: '',
    tjjsrq: '',
    shlxdm: '',
    czrbh: '',
    czrxm: '',
  },
})

// 表格数据
const tableData = ref<TableItem>({
  data: [],
  titles: [
    {
      label: '案件名称',
      prop: 'ajmc',
      width: 200,
      custom: true,
      sort: false,
      dqfs: 'left',
    },
    { label: '案卷类别', prop: 'ajlx', width: 200, dqfs: 'left' },

    { label: '部门受案号', prop: 'bmsah', width: 260, dqfs: 'left' },
    { label: '统一受案号', prop: 'tysah', width: 200, dqfs: 'left' },
    { label: '承办部门', prop: 'cbbmmc', width: 120, dqfs: 'left' },
    { label: '检察官', prop: 'cbjcg', width: 120, dqfs: 'left' },
    { label: '归档人', prop: 'gdr', width: 120, dqfs: 'left' },
    { label: '提交日期', prop: 'gdtjrq', width: 130, dqfs: 'left' },
    { label: '案卷数量', prop: 'ajsl', width: 120, dqfs: 'left' },
    { label: '总页数', prop: 'zys', width: 120, dqfs: 'left' },
    { label: '纸质页数', prop: 'zzys', width: 120, dqfs: 'left' },
    { label: '音像', prop: 'sxsl', width: 120, dqfs: 'left' },
    {
      label: '审核日期',
      prop: 'shrq',
      width: 160,
      dqfs: 'left',
      format: 'YYYY-MM-DD HH:mm:ss',
    },
    {
      label: '审核结论',
      prop: 'shjg',
      width: 120,
      format: {
        '1': '审核通过',
        '2': '审核退回',
      },
      dqfs: 'left',
    },
    { label: '原因', prop: 'shthyymc', width: 120, dqfs: 'left' },
  ],
})

/*
 * 分页
 */
const changePageNum = (val: number) => {
  formData.value.pageNum = val
  getTableData()
}

/*
 * 分页
 */
const changePageSize = (val: number) => {
  formData.value.pageNum = 1
  formData.value.pageSize = val
  getTableData()
}

/*
 * 查询按钮
 */
const searchTable = (data: any) => {
  let newObj: any = {}
  for (const key in data) {
    if (key.endsWith('-rq')) {
      const kssj = key.replace('-rq', 'ksrq')
      const jssj = key.replace('-rq', 'jsrq')
      if (data[key]?.length) {
        newObj[kssj] = data[key][0]
        newObj[jssj] = data[key][1]
      } else {
        newObj[kssj] = ''
        newObj[jssj] = ''
      }
    }
    newObj[key] = data[key]
  }
  formData.value.filter = newObj
  getTableData()
}

/*
 * 获取表格数据
 */
const tableBoxRef = ref<InstanceType<typeof tableBox>>()
const getTableData = () => {
  formData.value.filter.shlxdm = '2'
  const params = formData.value
  loading.value = true
  gdglService
    .queryGdyshList(params)
    .then((res) => {
      tableData.value.data = res.data.list
      total.value = res.data.total
    })
    .catch((err) => {
      !err.isError && ElMessage({ type: 'warning', message: err.message })
    })
    .finally(() => {
      loading.value = false
    })
}

/**
 * 表格选择
 */
const multipleSelection = ref<any[]>([])
const selectionChange = (val: any[]) => {
  multipleSelection.value = val
}

const optionsObj = ref<any>({})

/*
 * 点击案件名称进入归档
 */
const checkRow = (row: any) => {
  router.push({
    path: '/jcgshxq',
    query: {
      ...row,
      route: '/dacy',
      jumpFrom: 'ysh',
    },
  })
}

/*
 * 打开归档跟踪弹框
 */
const openGdgzDialog = (row: any) => {
  tableBoxRef.value?.openGdgzDialog(row.gdbh)
}

/*
 * 打开归档登记表
 */
const openGddjbDialog = (arr: any) => {
  tableBoxRef.value?.openWsDialog(arr)
}

const rowClick = (row: any) => {
  if (multipleSelection.value?.includes(row)) {
    tableBoxRef.value?.refTable.toggleRowSelection(row, false)
  } else {
    tableBoxRef.value?.refTable.toggleRowSelection(row, true)
  }
}

defineExpose({ changePageNum })
</script>
<template>
  <div class="container" v-loading="loading">
    <!--表单-->
    <div class="form-box">
      <form-box
        ref="formBoxRef"
        :formLabel="formLabel"
        :formData="formData.filter"
        :moreShow="false"
        :optionsObj="optionsObj"
        @searchEvent="searchTable"
      />
    </div>

    <!--按钮组-->
    <div class="btn-box">
      <div class="table-title">
        <span>待审核列表</span>
      </div>
      <button class="btn_operation" @click="changePageNum(1)">
        <div class="btn-refresh"></div>
        刷新
      </button>
    </div>

    <!--表格-->
    <div class="table-box">
      <table-box
        gnbm="dacysh-ysh"
        ref="tableBoxRef"
        :table-data="tableData"
        :selectShow="true"
        :showExport="false"
        :formParams="formData"
        :getTableDataParam="page"
        @changeCurrent="changePageNum"
        @changeSize="changePageSize"
        @selectionChange="selectionChange"
        @checkRow="checkRow"
        @rowClick="rowClick"
      >
        <template #myAction>
          <el-table-column label="操作" width="100" fixed="right">
            <template #default="scope">
              <div class="active-img" title="归档跟踪" @click="openGdgzDialog(scope.row)">
                <img src="@/assets/images/gdgztext.png" />
              </div>
              <div class="active-img" title="归档登记表" @click="openGddjbDialog([scope.row])">
                <img src="@/assets/images/gddjb.png" />
              </div>
              <!-- <div class="active-img" title="检测结果">
                <div class="btn-jcjg"></div>
              </div> -->
            </template>
          </el-table-column>
        </template>
      </table-box>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .form-box {
    width: 100%;
  }
  .btn-box {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-bottom: 10px;
  }

  .table-box {
    flex: 1;
    flex-shrink: 0;
    overflow: auto;
  }

  .active-img {
    cursor: pointer;
    float: left;
  }
  .active-img + .active-img {
    margin-left: 10px;
  }
}
</style>
